package cn.edu.scau.cmi.linzhiyang.userInterface;

import cn.edu.scau.cmi.linzhiyang.client.SafeCompositeClient;
import cn.edu.scau.cmi.linzhiyang.composite.safe.Member;
import cn.edu.scau.cmi.linzhiyang.composite.safe.Organization;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;

public class SafeCompositeController {
	@FXML
	private TextField firstTextField;

	@FXML
	private TextField secondTextField;

	@FXML
	private RadioButton personRadioButton;

	@FXML
	private RadioButton orgRadioButton;

	@FXML
	private ChoiceBox<String> funcChoiceBox;

	@FXML
	private TextArea outputTextArea;

	@FXML
	private ToggleGroup choice;

	@FXML
	private Button confirmButton;

	private SafeCompositeClient mainApp;

	private Organization root;

	@FXML
	private void initialize() {
		funcChoiceBox.setItems(FXCollections.observableArrayList("添加对象", "删除对象", "查看对象"));
		root = new Organization("Root");
	}

	public void setMainApp(SafeCompositeClient mainApp) {
		this.mainApp = mainApp;
	}

	@FXML
	public void handleConfirmButton() {
		String first = firstTextField.getText();
		String second = secondTextField.getText();
		boolean person = personRadioButton.isSelected();
		boolean org = orgRadioButton.isSelected();
		String func = funcChoiceBox.getValue();

		if (first.isEmpty() || func.isEmpty() || second.isEmpty()) {
			Alert alert = new Alert(AlertType.ERROR);
			alert.setHeaderText("不能为空！！！");
			alert.setContentText("请重新输入！！！");
			alert.showAndWait();
		} else {
			switch (func) {
			case "添加对象":
				if (first.equals("root")) {
					if (personRadioButton.isSelected()) {
						if (root.addMember(new Member(second))) {
							System.out.println("yes");
						} else {
							System.out.println("no");
						}
					} else if (org) {
						if (root.addMember(new Organization(second))) {
							System.out.println("yes");
						} else {
							System.out.println("no");
						}
					}
					outputTextArea.setText("添加成功！！！");
					break;
				}
				for (int i = 0; i < root.getList().size(); i++) {
					if (root.getList().get(i).getName() == first) {
						if (person) {
							((Organization) root.getList().get(i)).addMember(new Member(second));
						} else if (org) {
							((Organization) root.getList().get(i)).addMember(new Organization(second));
						}
					}
				}
				outputTextArea.setText("添加成功！！！");
				break;
			case "删除对象":
				if (first.equals("root")) {
					if (person) {
						root.delMember(new Member(second));
					} else if (org) {
						root.delMember(new Organization(second));
					}
					outputTextArea.setText("删除成功！！！");
					break;
				}
				for (int i = 0; i < root.getList().size(); i++) {
					if (root.getList().get(i).getName() == first) {
						if (person) {
							((Organization) root.getList().get(i)).delMember(new Member(second));
						} else if (org) {
							((Organization) root.getList().get(i)).delMember(new Organization(second));
						}
					}
				}
				outputTextArea.setText("删除成功！！！");
				break;
			case "查看对象":
				String str = null;
				if (first.equals("root")) {
					str = root.getMembers();
					outputTextArea.setText(str);
					break;
				}
				for (int i = 0; i < root.getList().size(); i++) {
					if (root.getList().get(i).getName() == first) {
						str = ((Organization) root.getList().get(i)).getMembers();
					}
				}
				outputTextArea.setText(str);
				break;
			}
		}
	}
}